package com.googlecode.cannedbeans.generator.util;

import com.googlecode.cannedbeans.generator.model.Constraint;
import com.googlecode.cannedbeans.generator.model.ConstraintType;
import java.lang.annotation.Annotation;
import org.hibernate.validator.Length;

/**
 * This class should be made so that it is easy to plug custom Annotation to Constraint mapping in.
 * @author Kim
 */
public class ConstraintExtractorImpl implements ConstraintExtractor{

    public Constraint getConstraint(Annotation annotation) {
        if (annotation.annotationType().isAssignableFrom(org.hibernate.validator.Length.class)) {
            Length length = (Length) annotation;
            Constraint constraint = new Constraint();
            constraint.setConstraintType(ConstraintType.LENGTH);
            if (length.max() != Integer.MAX_VALUE) {
                constraint.addProperty("max", length.max());
            }
            if (length.min() != 0) {
                constraint.addProperty("min", length.min());
            }
            return constraint;
        } else if (annotation.annotationType().isAssignableFrom(org.hibernate.validator.NotNull.class)) {
            Constraint constraint = new Constraint();
            constraint.setConstraintType(ConstraintType.NOTNULL);
            return constraint;
        } else {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

}
